package com.plume.toolbox.plus.ui.slideshow;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.plume.toolbox.plus.LoginActivity;
import com.plume.toolbox.plus.UserInfoActivity;
import com.plume.toolbox.plus.UserInfoSaver;
import com.plume.toolbox.plus.databinding.FragmentSlideshowBinding;
import com.plume.toolbox.plus.ui.DisclaimerDialogFragment;

public class SlideshowFragment extends Fragment {

    private FragmentSlideshowBinding binding;
    private Handler mHandler = new Handler(Looper.getMainLooper());
    
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentSlideshowBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final Button button_login = binding.textSlideshow;
        button_login.setOnClickListener(v -> {
            SharedPreferences prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
            boolean agreedToDisclaimer = prefs.getBoolean("agreed_to_disclaimer", false);
            if(!agreedToDisclaimer){
                showToast("您必须同意用户协议才能使用登录功能");
                DisclaimerDialogFragment dialogFragment = new DisclaimerDialogFragment();
                dialogFragment.show(getActivity().getSupportFragmentManager(), "disclaimer_dialog");
            }else{       
            showLoginDialog();}
        });
        final Button button_info = binding.btnUserinfo;
        button_info.setOnClickListener(v -> {
            if(!UserInfoSaver.checkLoginStatus(getActivity())){
                showToast("不是哥们儿，你还没登录");
            }else{    
            showInfoActivity();
             }       
        });
       // slideshowViewModel.getText().observe(getViewLifecycleOwner(), textView::setText);
        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
    
    private void showLoginDialog() {
        LoginActivity loginDialog = new LoginActivity();
        loginDialog.show(getChildFragmentManager(), "LoginDialog");
    }
    
    private void showInfoActivity(){
        Intent intent=new Intent(getActivity(),UserInfoActivity.class);
        startActivity(intent);
    }
    
    public void showToast(final String message) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }
}